
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;
using Swaf.Container;

namespace eCom.Gui
{
	public class eComProductBizProc : Articles.Gui.ArticleManagementBizProc
	{
		public eComProductBizProc(ActiveBizProc procState) : base(procState)
		{
			m_canChangeDetailsBizRule = false;
		}
		
		protected override void setupRuleNames()
		{
			base.setupRuleNames();
			m_getBizObjListRule = "getProducts";
			m_getBizObjDetailsRule = "getProductDetails";
			m_updateBizObjRule = "updateProduct";
			m_deleteBizObjRule = "deleteProduct";

			string prodType = m_procState.get("objectTreeName") as string;
			if(prodType == null || prodType.Length == 0)
				m_procState.put("objectTreeName", "eComProd");
		}

		protected override bool shouldCallGetBizObjDetailsRule(object[] ruleParams)
		{
			return ruleParams != null && ruleParams[0] != null;
		}

		public void setAsChild(object artInfo)
		{
			IBizObj obj = CurrentBizObj;
			if (obj != null && artInfo != null)
			{
				FlexiMap info = new FlexiMap();
				info.put("relatedItemId", obj.get("id"));
				info.put("articleId", artInfo);
				info.put("associationName", "eComProd");
				IResults rc = App.ruleMgr.execute("createArticleAssociation", info);
			}
			else
				m_procState.BPC.Messenger.add("Error-ArticleAssociationException", null);
		}
		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("SetAsChildArticle", true, (CurrentEditPrivilege > Privileges.Edit), this, new ActionParamTrigger(setAsChild), new object[] { }));
		}

		public override void updateActionState()
		{
			base.updateActionState();
			((GenericAction)Actions["SetAsChildArticle"]).Availability = CanEdit ? ActionAvailability.enabled : ActionAvailability.none;
		}
		public override Privileges CurrentEditPrivilege
		{
			get { return CurrentPrincipal.getPrivilege("AppFunctions.ManageProducts"); }
		}

		public override IBizObj createNewBizObj()
		{
			IBizObj newOne =  setupNewBizObj(App.bizObjMgr.create(BizObjTypeName));
			newOne.put("treeName", "eComProd");
			return newOne;
		}

		protected override string BizObjTypeName
		{
			get
			{
				return "eComProduct";
			}
		}

		protected override object[] getUpdateBizRuleParameters(IBizObj bizObj)
		{
			return new object[] { bizObj };
		}

		protected override object[] getDetailsBizRuleParameters(object bizObjKey)
		{
			object[] p = base.getDetailsBizRuleParameters(bizObjKey);
			object[] p2 = new object[p.Length - 1];
			Array.Copy(p, p2, p2.Length);
			return p2;
		}

		protected override void getBizObjList()
		{
			base.getBizObjList();
			if (m_bizobjs != null && m_bizobjs.numElements == 0)
				m_bizobjs.Add(createNewBizObj());
		}
	}
}
